clear all
clc

tic
%add weka to path
%javaaddpath('C:\Program Files\Weka-3-6\weka.jar') %for Masahiro probablly for MJ, too
javaaddpath('/Applications/weka-3-6-10/weka.jar')%for Ben
%load hog function
run('toolbox/vl_setup')
disp('Opening files...')
vehicles=read_vehicles();
t=toc;
disp([StoHMS(t),': Finished opening files, calculating Features...'])
for i=1:size(vehicles,2)
    vehicles(i).calculate_wave_features('haar',3)
    vehicles(i).calculate_hog(15);
end

a=1/0;
t=toc;
inds=ones(1,9);
disp([StoHMS(t),': Finished calculating angle Features, Exporting arff...'])
writeFeaturesToArff(vehicles, 'hog', 'angle', 'hog.arff');
writeFeaturesToArff(vehicles, 'wav', 'angle', 'wav.arff');
t=toc;
disp([StoHMS(t),': Finished writing arff. Python->InfoGain'])
[status, cmdout]=system('python Python/main.py'); %for Ben
%[status, cmdout]=system('python Python\main.py'); %for Masahiro, MJ
t=toc;
disp([StoHMS(t),': Finished InfoGain Calculation, Selecting...'])
ind=findstr(cmdout, sprintf('\n'));
hog_select=strread(cmdout(1:ind-1),'%u','delimiter',', ');
wav_select=strread(cmdout(ind+1:end),'%u','delimiter',', ');
hog_select=hog_select(1:end-1)+1;
wav_select=wav_select(1:end-1)+1;
for i=1:size(vehicles,2)
    vehicles(i).selected_wave_features=vehicles(i).wave_features(wav_select);
    vehicles(i).selected_hog_features=vehicles(i).hog_featuers(wav_select);
end

t=toc;
disp([StoHMS(t),': Finished Selecting angle features, on to type features...'])

front=[];
side=[];
back=[];
for i=1:size(vehicles, 2)
    if vehicles(i).camera_angle(1)=='f'
        front(end+1)=i;
    elseif vehicles(i).camera_angle(1)=='s'
        side(end+1)=i;
    elseif vehicles(i).camera_angle(1)=='b'
        back(end+1)=i;
    end
end
for j=1:3
    if j==1
        angle=front;
    elseif j==2
        angle=side;
    elseif j==3
        angle=back;
    end
        
writeFeaturesToArff(vehicles(angle), 'hog', 'type', 'hog.arff');
writeFeaturesToArff(vehicles(angle), 'wav', 'type', 'wav.arff');
t=toc;
disp([StoHMS(t),': Finished writing arff. Python->InfoGain'])
[status, cmdout]=system('python Python/main.py'); %for Ben
%[status, cmdout]=system('python Python\main.py'); %for Masahiro, MJ
t=toc;
disp([StoHMS(t),': Finished InfoGain Calculation, Selecting...'])
ind=findstr(cmdout, sprintf('\n'));
hog_select=strread(cmdout(1:ind-1),'%u','delimiter',', ');
wav_select=strread(cmdout(ind+1:end),'%u','delimiter',', ');
hog_select=hog_select(1:end-1)+1;
wav_select=wav_select(1:end-1)+1;
for i=1:size(front,2)
    vehicles(angle(i)).selected_wave_features2=vehicles(angle(i)).wave_features(wav_select);
    vehicles(angle(i)).selected_hog_features2=vehicles(angle(i)).hog_featuers(wav_select);
    vehicles(angle(i)).wave_features=[];
    vehicles(angle(i)).hog_featuers=[];
end
end

t=toc;
disp([StoHMS(t),': Finished Selecting angle features, on to type features...'])


t=toc;
disp([StoHMS(t),': Finished Selecting, writing selected features to arff'])
writeFeaturesToArff(vehicles, 'selected', 'angle', 'selected.arff');
save('vehicles_short','vehicles')

t=toc;
disp([StoHMS(t),': done'])

